package com.qyl.deguru.controller.customer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qyl.deguru.common.PageConstant;
import com.qyl.deguru.dto.Shop;
import com.qyl.deguru.dto.ShopType;
import com.qyl.deguru.dto.User;
import com.qyl.deguru.service.ShopService;
import com.qyl.deguru.service.ShopTypeService;
import com.qyl.deguru.service.UserService;
import com.qyl.deguru.service.impl.ShopServiceImple;
import com.qyl.deguru.service.impl.ShopTypeServcieImple;
import com.qyl.deguru.service.impl.UserServiceImpl;
import com.qyl.deguru.utils.MailUtils;
import com.qyl.deguru.utils.RadomCodeUtil;

/**
 * 前台登录controller
 * @author Administrator
 *
 */

@Controller
@RequestMapping("/customer")
public class UserController {
	ShopService shopService=new ShopServiceImple();
	ShopTypeService shopTypeService=new ShopTypeServcieImple();
	UserService userService=new UserServiceImpl();
	//跳转到home页面
	@RequestMapping("/home")
	public String toHome(ModelMap modelMap){
		//获取轮播图下的商品图片
		List<Shop> shops=shopService.getShop();
		List<Shop> shops1=shopService.getShopByisHot();
		List<Shop> shops2=shopService.getShopByisHot2();
		List<Shop> shops3=shopService.getShopByisHot3();
		List<Shop> shops4=shopService.getShopByisHot4();
		List<Shop> shops5=shopService.getShopByisHot5();
		//获取商品类型【4位】
		List<ShopType> shopType=shopTypeService.getShopTypeLimit4();
		modelMap.addAttribute("shopType", shopType);
		modelMap.addAttribute("shops",shops);
		modelMap.addAttribute("shops1",shops1);
		modelMap.addAttribute("shops2",shops2);
		modelMap.addAttribute("shops3",shops3);
		modelMap.addAttribute("shops4",shops4);
		modelMap.addAttribute("shops5",shops5);
		return "home";
	}
	//跳转到登陆界面
	@RequestMapping("/toLogin")
	public String toLogin(){
		return "/login";
	}
	//用户登陆
	@RequestMapping("/customerLogin")
	@ResponseBody
	public Map<String, String> adminLogin(String user_id,String pass_word,String code,HttpSession session){
		Map<String, String> map = new HashMap<String, String>();
		//从session中获得验证码
		String adminCode = (String) session.getAttribute(PageConstant.AUTH_CODE);
		if(adminCode.equals(code)){
			//验证码相同，从数据库获得用户
			User user = userService.getUserByUserIdAndPwd(user_id, pass_word);
			if(null !=user){
				if(user.getRole()>1){
					session.setAttribute(PageConstant.SESSION_USER, user);
					map.put("status", "success");
				}else{
					//用户权限不足
					map.put("status", "fail");
					map.put(PageConstant.TIP, "用户权限不足");
				}
			}else{
				//用户或者密码错误
				map.put("status", "fail");
				map.put(PageConstant.TIP, "用户或者密码错误");
			}			
		}else{
			//验证码错误
			map.put("status", "fail");
			map.put(PageConstant.TIP, "验证码错误");
		}		
		return map;	
	}
	//退出
	@RequestMapping("/exit")
	public String exit(HttpSession session){
		//注销用户
		session.removeAttribute(PageConstant.SESSION_USER);
		return "forward:home";
	}
	//注册
	@RequestMapping("/toRegister")
	public String toRegister(){		
		
		return "reg";
	}
	//用户注册
	@RequestMapping("/register")
	public String register(User user,ModelMap modelMap) throws Exception{
		//生成激活码
		String active_code=RadomCodeUtil.radomCode();
		//发送邮件
		if(!user.getEmail().isEmpty()){
			//定义邮件标题
			String title="Deguru激活帐户";
			//定义邮件内容
			StringBuilder content=new StringBuilder();
			content.append("尊敬的"+user.getUser_id()+":<br/>");
			content.append("欢迎登陆，请点击<a href='http://127.0.0.1:8080/deguru/customer/activeCode?code="+active_code+"'>激活码</a>，激活帐户！！"+active_code);
			//发送邮件
			MailUtils.sendMail("1182891631@qq.com", title, content.toString());
		}
		//用户注册
		user.setRole(2);
		user.setActive_code(active_code);
		userService.registerUser(user);
		modelMap.put(PageConstant.TIP, "注册成功，请登陆！！");
		return "login";
	}
	//激活用户
	@RequestMapping("/activeCode")
	public String activeCode(String code,ModelMap modelMap){
		int count=userService.setActiveStatus(code);
		if(count>0){
			modelMap.put(PageConstant.TIP, "激活成功");
		}else{
			//用户权限不足
			modelMap.put(PageConstant.TIP, "激活失败");
		}
		
		return "login";
	}
	//修改用户信息
	@RequestMapping("/toUpdateData")
	public String updateData(int uid){
		return "updateData";
	}
	@RequestMapping("/getUserByEmail")
	@ResponseBody
	public Map<String,String> getUserByEmail(String email){
		
		Map<String,String> map = new HashMap<String, String>();
		User user = userService.getUserByEmail(email);
		if(user != null ){
			map.put(PageConstant.TIP, "邮箱可用");
		}else{
			map.put(PageConstant.TIP, "邮箱不存在");
		}		
		return map;
	}
	
}
